import java.util.Scanner;

public class PasswordManager {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        boolean isRunning = true;

        while (isRunning) {
            System.out.println("请选择操作：");
            System.out.println("1. 加密功能");
            System.out.println("2. 解密功能");
            System.out.println("3. 判断密码强度");
            System.out.println("4. 密码生成");
            System.out.println("5. 退出");
            int choice = scanner.nextInt();

            switch (choice) {
                case 1:
                    encryptPassword();
                    break;
                case 2:
                    decryptPassword();
                    break;
                case 3:
                    checkPasswordStrength();
                    break;
                case 4:
                    generatePassword();
                    break;
                case 5:
                    isRunning = false;
                    break;
                default:
                    System.out.println("无效的选项，请重新输入！");
                    break;
            }
        }

        scanner.close();
    }

    public static void encryptPassword() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要加密的密码：");
        String password = scanner.nextLine();

        StringBuilder encryptedPassword = new StringBuilder();

        // 遍历密码的每个字符
        for (int i = 0; i < password.length(); i++) {
            char c = password.charAt(i);
            // ASCII码加上位置和偏移值
            int encryptedChar = (int)c + i + 1 + 3;
            encryptedPassword.append((char)encryptedChar);
        }
        // 调换第一位和最后一位
        if (encryptedPassword.length() > 1) {
            char firstChar = encryptedPassword.charAt(0);
            char lastChar = encryptedPassword.charAt(encryptedPassword.length() - 1);
            encryptedPassword.setCharAt(0, lastChar);
            encryptedPassword.setCharAt(encryptedPassword.length() - 1, firstChar);
        }
        // 反转字符串
        encryptedPassword.reverse();

        System.out.println("加密后的密码：" + encryptedPassword.toString());
    }

    public static void decryptPassword() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要解密的密码：");
        String encryptedPassword = scanner.nextLine();
        StringBuilder password = new StringBuilder(encryptedPassword);
        // 反转字符串
        password.reverse();
        // 调换第一位和最后一位
        if (password.length() > 1) {
            char firstChar = password.charAt(0);
            char lastChar = password.charAt(password.length() - 1);
            password.setCharAt(0, lastChar);
            password.setCharAt(password.length() - 1, firstChar);
        }
        StringBuilder decryptedPassword = new StringBuilder();
        // 遍历密码的每个字符
        for (int i = 0; i < password.length(); i++) {
            char c = password.charAt(i);

            // ASCII码减去位置和偏移值
            int decryptedChar = (int)c - i - 1 - 3;

            decryptedPassword.append((char)decryptedChar);
        }
        System.out.println("解密后的密码：" + decryptedPassword.toString());
    }

    public static void checkPasswordStrength() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要判断强度的密码：");
        String password = scanner.nextLine();

        boolean hasLowerCase = false;
        boolean hasUpperCase = false;
        boolean hasNumber = false;

        // 判断密码强度
        if (password.length() < 8) {
            System.out.println("该密码的强度为：弱");
            return;
        }

        for (int i = 0; i < password.length(); i++) {
            char c = password.charAt(i);

            if (Character.isLowerCase(c)) {
                hasLowerCase = true;
            } else if (Character.isUpperCase(c)) {
                hasUpperCase = true;
            } else if (Character.isDigit(c)) {
                hasNumber = true;
            }
        }

        if (hasLowerCase && hasUpperCase && hasNumber) {
            System.out.println("该密码的强度为：高");
        } else if (hasLowerCase || hasUpperCase || hasNumber) {
            System.out.println("该密码的强度为：中");
        } else {
            System.out.println("该密码的强度为：弱");
        }
    }

    public static void generatePassword() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入密码长度：");
        int length = scanner.nextInt();

        StringBuilder password = new StringBuilder();

        // 生成密码
        for (int i = 1; i <= length; i++) {
            // 生成随机字符（数字或大小写字母）
            int randomNum = (int)(Math.random() * 62);
            char c;
            if (randomNum < 10) {
                c = (char)('0' + randomNum);
            } else if (randomNum < 36) {
                c = (char)('A' + randomNum - 10);
            } else {
                c = (char)('a' + randomNum - 36);
            }
            password.append(c);
        }

        System.out.println("生成的密码为：" + password.toString());
    }
}
